/* ------------------------------------------------------------------------------------
	开始
------------------------------------------------------------------------------------ */

// 导入express
const express = require('express')

// 创建服务器
const app = express()

// process.cwd() 返回app.js所在目录

/* ------------------------------------------------------------------------------------
	自定义中间件
------------------------------------------------------------------------------------ */

// 自定义模块 - resextra
const resextra = require('./util/resextra.js')
app.use(resextra)

// 自定义模块 - execSql
const execSql = require('./util/sql.js')
app.use(execSql)

// 自定义模块 - getById
const getById = require('./util/getById.js')
app.use(getById)

/* ------------------------------------------------------------------------------------
	第三方中间件
------------------------------------------------------------------------------------ */
// 静态资源托管
app.use('/uploads', express.static('./uploads'))

// 开启 cors 跨域
const cors = require('cors')
app.use(cors())
/* 
// 添加token验证
const jwt = require('express-jwt')
app.use(jwt({
	secret: 'token', // 生成token时的 钥匙，必须统一
	algorithms: ['HS256'] // 必填，加密算法，无需了解
}).unless({
	path: ['/user'] // 排除接口
}))
 */
// 处理 post 参数
app.use(express.urlencoded())
app.use(express.json())

/* ------------------------------------------------------------------------------------
	路由
------------------------------------------------------------------------------------ */
app.use('/', (res, req, next) => {
	console.log(req)
	// res.send(req)
	next()
})
app.use('/api/private/v1/banner', require('./routes/api/private/v1/banner.js'))
app.use('/api/private/v1/categories', require('./routes/api/private/v1/categories.js'))
app.use('/api/private/v1/shops', require('./routes/api/private/v1/shops.js'))
app.use('/api/private/v1/goods', require('./routes/api/private/v1/goods.js'))
app.use('/api/private/v1/users', require('./routes/api/private/v1/users.js'))
app.use('/api/private/v1/upload', require('./routes/api/private/v1/upload.js'))
app.use('/api/private/v1/notice', require('./routes/api/private/v1/notice.js'))
app.use('/api/private/v1/tabs', require('./routes/api/private/v1/tabs.js'))
app.use('/api/private/v1/product', require('./routes/api/private/v1/product.js'))
app.use('/api/private/v1/login', require('./routes/api/public/v1/login.js'))
app.use('/api/private/v1/msg', require('./routes/api/public/v1/message.js'))

/* ------------------------------------------------------------------------------------
	结尾
------------------------------------------------------------------------------------ */

// 异常处理
app.use((err, req, res) => {
	if (err) return res.sendResult(null, 500, err)
})

// 开启端口
const port = 9122
app.listen(port, () => console.log(port + '服务器启动成功提示'))
